package com.hospital.dao.impl;

import com.hospital.model.Admin;
import com.hospital.util.DruidUtil;
import com.hospital.util.LoggerUtil;
import org.slf4j.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 管理员数据访问对象
 * 处理与管理员相关的数据库操作，包括登录验证等功能
 */
public class AdminDao {
    
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerUtil.getLogger();
    
    /**
     * 管理员登录验证
     * @param username 用户名
     * @param password 密码
     * @return Admin对象，如果登录失败返回null
     */
    public Admin login(String username, String password) {
        String sql = "SELECT id, username, password, name FROM admin WHERE username = ? AND password = ?";
        Admin admin = null;
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            
            ps.setString(1, username);
            ps.setString(2, password);
            
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    admin = new Admin();
                    admin.setId(rs.getInt("id"));
                    admin.setUsername(rs.getString("username"));
                    admin.setPassword(rs.getString("password"));
                    admin.setName(rs.getString("name"));
                }
            }
        } catch (SQLException e) {
            logger.error("管理员登录验证失败", e);
        }
        return admin;
    }

} 